﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using CoreMobile.bce;
using CoreMobile.cfg;
using CoreMobile.net;
using Opticon.Bcrctl;
using VABMobile.oo;
using VABMobile.cfg;

namespace VABMobile.ui
{
    public partial class LoginForm : Form, IBCENotifiable
    {
        private BCEReader reader;
        private static string VALID_CODE = "1";
        private static string VALID_CFG_CODE = "vab.easy";
        private Persona auxiliar;
        private SettingsManager<VABSettings> settingsManager;
        private Dictionary<string,string> enterprises; 

        public LoginForm()
        {
            InitializeComponent();
            
            
            reader = BCEReader.getReaderWith(this);
            settingsManager = SettingsManager<VABSettings>.get();

            this.createEntrepriseNameDictionary();
            
        }

        private void createEntrepriseNameDictionary()
        {
            enterprises = new Dictionary<string, string>(5);
            enterprises["CDS"] = "Cruz del Sur";
            enterprises["SUR"] = "Pullman Sur";
            enterprises["TJA"] = "Bus Norte Intl.";
            enterprises["NAV"] = "Turibus Intl.";
            enterprises["TCH"] = "Transchiloé";

            this.empresas.Items.Clear();
            foreach (string enterpriseName in enterprises.Values)
                this.empresas.Items.Add(enterpriseName);

            this.empresas.SelectedItem = enterprises[settingsManager.Settings.EnterpriseName];
        }

        private void initTurnoMenu_Click(object sender, EventArgs e)
        {
            if(auxiliar==null)
            {
                MessageBox.Show("Debe ingresar un usuario");
                return;
            }
            if(string.IsNullOrEmpty(codigo.Text) ||
                codigo.Text!=VALID_CODE)
            {
                MessageBox.Show("Debe ingresar codigo valido");
                return;
            }
            settingsManager.Settings.EnterpriseName = enterprises.Keys.FirstOrDefault(
                    key => enterprises[key].Equals(empresas.SelectedItem));
            (new RegViajeForm(auxiliar)).Show();
            this.Hide();
            reader.BarcodeDecoder.DataHandler -= this.receiverDataFromReader;
        }

        // exit

        public void receiverDataFromReader(object sender, BarcodeDecoder.ScanEventArgs e)
        {
            string data=reader.preProcess(e);

            //MessageBox.Show(data);

            if(string.IsNullOrEmpty(data)) return;
            
            if(e.CodeType != BarcodeDecoder.CodeIdTypes.PDF417)
            {
                //MessageBox.Show("Necesita leer un CI del Reg. Civil");
                return;
            }

            auxiliar=new Persona(BCEReader.decodePDF417RegCivil(data)[0]); //rut
            this.usuario.Text = auxiliar.ppRut();
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void configMenuItem_Click(object sender, EventArgs e)
        {
            if (this.codigo.Text!=VALID_CFG_CODE)
            {
                MessageBox.Show("Codigo cfg invalido");
                return;
            }

            var cfg=new ConfigForm();
            cfg.ServerIp.Text = settingsManager.Settings.ServerIp;
            cfg.ServerPort.Text = "" + settingsManager.Settings.ServerPort;
            cfg.PrinterPort.SelectedIndex = settingsManager.Settings.PrinterPort;

            if(DialogResult.OK == cfg.ShowDialog())
            {
                settingsManager.Settings.ServerIp = cfg.ServerIp.Text;
                settingsManager.Settings.ServerPort = Int32.Parse(cfg.ServerPort.Text);
                settingsManager.Settings.PrinterPort = cfg.PrinterPort.SelectedIndex;
                settingsManager.save();
            }
        }
    }
}